package org.neuedu.ssmp.crm.util;
import lombok.extern.slf4j.Slf4j;
import org.neuedu.ssmp.crm.entity.Menuitem;
import org.neuedu.ssmp.crm.entity.SysRoleAuthor;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
public class MenuTreeUtil {
    private List<SysRoleAuthor> protoList; //原型数据
    private List<SysRoleAuthor> list = new ArrayList<>();//树结构数据集合

    //获得树型结构数据
    public List<SysRoleAuthor> getMenuTree(List<SysRoleAuthor> protoList) {
        this.protoList = protoList;
        for (SysRoleAuthor item : protoList) {
            if (Objects.equals(item.getLevel(), 0)) {
                list.add(loadingData(item));
            }
        }
        return list;
    }

    //填充数据
    public SysRoleAuthor loadingData(SysRoleAuthor item) {
        return item.setChildren(getTreeChild(item));
    }

    //查找子节点
    public List<SysRoleAuthor> getTreeChild(SysRoleAuthor parentItem) {
        List<SysRoleAuthor> childList = new ArrayList<>();
        for (SysRoleAuthor item: protoList) {
            if (item.getCode().startsWith(parentItem.getCode()) && item.getLevel() - parentItem.getLevel() == 1) {
                childList.add(loadingData(item));
            }
        }
        return childList;
    }
}
